package cn.ulc.socket.admin.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.annotation.IgnoreAuth;
import cn.ulc.common.util.Result;
import cn.ulc.socket.admin.model.param.account.ChangePasswordParam;
import cn.ulc.socket.admin.model.param.account.LoginDetailParam;
import cn.ulc.socket.admin.service.common.AccountServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/account")
public class AccountCtrl {
    private static final Log log = LogFactory.get();

    @Autowired
    AccountServ accountServ;

    @IgnoreAuth
    @PostMapping("/login")
    public Result findList(@Validated @RequestBody LoginDetailParam param, BindingResult result) {
        if(result.hasErrors()) {
            log.error("登录错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(accountServ.login(param));
    }

    @GetMapping("/userinfo")
    public Result getUserInfo(@Validated String token) {
        return Result.success(accountServ.findUserInfo(token));
    }

    @PutMapping("/changePassword")
    public Result changePassword(@Validated @RequestBody ChangePasswordParam param){
        return Result.success(accountServ.changePassword(param));
    }
}
